package com.saret.crawler.type;

import com.saret.utils.StringUtils;
import org.htmlparser.parserapplications.filterbuilder.layouts.NullLayoutManager;
import org.json.*;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * User: biniam.gebremichael
 * Date: 2/3/11
 */
public class GUrlImpl implements GUrl, Comparable<GUrl> {

    String url;
    Integer size;
    String caption;
    String type;
    String keyWord;
    String filter;
    static String regEx = "^(.*)\\s+site:(.*)$";

    public GUrlImpl(String url, Integer size, String caption, String type, String keyWord, String filter) {
        this.url = url;
        this.size = size;
        this.caption = caption;
        this.type = type;
        this.keyWord = keyWord;
        this.filter = filter;
    }

    public GUrlImpl(String url, Integer size, String caption, String type, String keyWordPlusFilter) {
        this(url, size, caption, type, keyWordPlusFilter, null);
        //  this(url, size, caption, type,
        //          StringUtils.match(keyWordPlusFilter, regEx, 0) != null ? StringUtils.match(keyWordPlusFilter, regEx, 1) : keyWordPlusFilter,
        //          StringUtils.match(keyWordPlusFilter, regEx, 0) != null ? StringUtils.match(keyWordPlusFilter, regEx, 2) : null);
    }

    public GUrlImpl(String url, String type) {
        this(url, 0, null, type, null);
    }

    public GUrlImpl(JSONObject jo) throws JSONException {
        this(jo.getString("url"), jo.getInt("size"), null, jo.getString("type"), jo.getString("keyWord"), null);
  //        this(jo.getString("url"), jo.getInt("size"), jo.getString("caption"), jo.getString("type"), jo.getString("keyWord"), jo.getString("filter"));
    }



    @Override
    public String getUrl() {
        return url;
    }

    public String getKeyWord() {
        return keyWord;
    }

    @Override
    public String toString() {
        return type + "\t" + url;
    }

    @Override
    public int compareTo(GUrl o) {
        return getUrl().compareTo(o.getUrl());
    }

    public String getType() {
        return type;
    }

    @Override
    public int hashCode() {
        return url.hashCode();
    }

    public String toJson() {

        JSONObject obj = new JSONObject();
        try {
            obj.put("url", url);
            obj.put("size", size);
            obj.put("caption", caption);
            obj.put("type", type);
            obj.put("keyWord", keyWord);
            obj.put("filter", filter);
            obj.put("date", System.currentTimeMillis());
            return obj.toString();
        } catch (JSONException e) {
            return null;
        }

    }
}
